<template>
  <div>
    <MyComponent ref="myComponent" />
    <button @click="generatePDF">导出Vue组件内容为PDF</button>
  </div>
</template>
<script>
import html2canvas from 'html2canvas';
import jsPDF from 'jspdf';
import MyComponent from './MyComponent.vue';

export default {
  components: {
    MyComponent
  },
  methods: {
    async generatePDF() {
      const component = this.$refs.myComponent.$el;
      const canvas = await html2canvas(component, { scale: 2 });
      const imgData = canvas.toDataURL('image/png');
      const pdf = new jsPDF();
      pdf.addImage(imgData, 'PNG', 0, 0);
      pdf.save('component-content.pdf')
    }
  }
}
</script>
<style lang="">

</style>